
<template>
  <base-breadcrumb :title="titleName">
    <searchData
            :search-data="searchArr"
            @search="search"
    />
    <a-card :bordered="false">
      <a-spin :spinning="spinning">
     <div class="mb12">
       <a-button type="primary" @click="exportExcel" :loading="exportLoading" v-btn-permission="'bm_036_exp_04'">导出节点完成情况</a-button>
     </div>
      <list-table
              ref="table"
              size="default"
              :columns="columns"
              :request="loadData"
              :isShowTotal="false"
              row-key="optimizationId"
              :statistics-config="{ use: true, totalDels, temp }"
      >
        <span
                slot="serial"
                slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <template slot="versionNumber" slot-scope="text, record, index">
          <a @click="openVision(record)">{{record.versionNumberName}}</a>
        </template>
        <template
                slot="action"
                slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleDels(record)" v-btn-permission="'bm_036_loo_03'">查看</a>
              <template>
                <a v-btn-permission="'bm_036_edi_01'"
                        v-if="((record.auditStatus === 'FILLING' || record.auditStatus === 'DRAFT'
                        || record.auditStatus==='REJECTED')&&record.versionNumber==1)&&hideBuquBtn()"
                        @click="handleEdit(record)"
                >{{record.auditStatus === 'FILLING' ? '填报' : '编辑'}}</a>
<!--                <a  @click="handleAdjust(record)">调整</a>-->
                <a v-if="record.versionNumber==1 && record.auditStatus==='PAST'"
                   v-btn-permission="'bm_036_adj_02'"
                   @click="handleAdjust(record)">调整</a>
                <a
                        v-if="(record.versionNumber > 1 && (record.auditStatus==='DRAFT' || record.auditStatus==='REJECTED' || record.auditStatus==='PAST'))&&hideBuquBtn()"
                        @click="handleAdjust(record)"
                >调整</a>
              </template>
            <template>
              <a @click="handleApproveDetail(record)" v-btn-permission="'bm_036_loo_03'"
                 v-if="record.auditStatus !=='FILLING'&&record.auditStatus !=='DRAFT'">审批信息</a>
            </template>
          </MoreButton>
        </template>
      </list-table>

      <ApprovalDetails
              :title="'审批详情'"
              :business-id="businessId"
              :visible="visibleApprove"
              @cancel="visibleApprove = false"
              @confirm="visibleApprove = false"
      />
      </a-spin>
    </a-card>
    <a-modal
            title="历史版本信息"
            :visible="versionvisible"
            width="60%"
            :footer="null"
            destroyOnClose
            @cancel="versionvisible=false"
    >
      <list-table
              ref="table"
              size="default"
              :columns="columns2"
              :request="loadVisionData"
              :isShowTotal="false"
              row-key="optimizationId"
              :showPage="false"
              :loading="loadingVersion"
      >
        <template slot="versionNumber" slot-scope="text, record, index">
          <a @click="openVision(record)">{{record.versionNumber}}</a>
        </template>
        <template
                slot="action"
                slot-scope="text, record">
          <a @click="lookHistory(record)">查看详情</a>
        </template>
      </list-table>
    </a-modal>
  </base-breadcrumb>
</template>

<script>
  import OrgTreeSelect from '@/components/OrgTreeSelect'
  import ApprovalDetails from '@/components/approvalDetails'
  import { masterWorkPlanByPage, masterWorkPlanCount, masterWorkPlanExportExcelByPage
  , masterWorkPlanFindListByMainHistoryList, masterWorkPlanChecked} from "@/api/businessAffairs/workPlan"
  const columns = [
    {
      title: '序号',
      width: '65px',
      scopedSlots: { customRender: 'serial' }
    },
    {
      title: '分公司',
      dataIndex: 'branchCompanyName'
    },
    {
      title: '项目名称',
      dataIndex: 'projectName'
    },
    {
      title: '得分',
      dataIndex: 'score'
    },
    {
      title: '填报人',
      dataIndex: 'creatorName'
    },
    {
      title: '填报时间',
      dataIndex: 'submitDate'
    },
    {
      title: '版本信息',
      dataIndex: 'versionNumberName',
      scopedSlots: { customRender: 'versionNumber' }
    },
    {
      title: '状态',
      width: '110px',
      dataIndex: 'auditStatusName'
    },
    {
      title: '操作',
      dataIndex: 'actions',
      width: '200px',
      fixed: 'right',
      scopedSlots: { customRender: 'action' }
    }
  ]
  const columns2 = [
    {
      title: '版本号',
      dataIndex: 'versionNumber',
      scopedSlots: { customRender: 'versionNumber' }
    },
    {
      title: '填报时间',
      dataIndex: 'submitDate',
    },
    {
      title: '操作',
      dataIndex: 'actions',
      width: '200px',
      fixed: 'right',
      scopedSlots: { customRender: 'action' }
    }
  ]
  const status = [
    { label: '待填报', value: 'FILLING' },
    { label: '待提交审批', value: 'DRAFT' },
    { label: '审核中', value: 'SUBMITTED' },
    { label: '审核通过', value: 'PAST' },
    { label: '审核不通过', value: 'REJECTED' }
  ]
  export default {
    name: 'a' + Date.now(),
    components: {
      ApprovalDetails,
    },
    data() {
      return {
        loadingVersion: false,
        columns,
        columns2,
        exportLoading: false,
        versionvisible: false,
        businessId: '',
        visibleApprove: false,
        spinning: false,
        searchArr: [
          {
            name: '所属单位',
            valueKey: 'searchQuery',
            type: OrgTreeSelect,
          },
          {
            name: '填报时间',
            value: 'startEndDate',
            type: 'dateRange'
          },
          {
            name: '状态',
            value: 'auditStatus',
            type: 'select',
            data: status
          }
        ],
        titleName: '',

        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          const requestParameters = Object.assign({}, parameter, { query: { ...this.queryParam } })
          this.getOptimizationTotal()
          return masterWorkPlanByPage(requestParameters).then((res) => {
            this.total = res.totalSize
            res.data.forEach(item=>{
              if(item.versionNumber) {
                item.versionNumberName = 'v' + item.versionNumber
              }
              item.auditStatusName = (status.find(el=> el.value === item.auditStatus)).label
            })
            return res
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        totalDels: {
          total: 0
        },
        row: {},
        total: 0,
        temp: () => {
          return (
            <span style="margin-right: 12px">
              总数：<span class="totalNum">{ this.total || 0 } 个</span> |
              待填报：<span class="totalNum">{this.totalDels.filling || 0 } 个</span> |
              待提交审批：<span class="totalNum">{ this.totalDels.draft || 0 } 个</span> |
              审批中：<span class="totalNum">{ this.totalDels.submitted || 0 } 个</span> |
              审批通过：<span class="totalNum">{ this.totalDels.past || 0 } 个</span> |
              审批不通过：<span class="totalNum">{ this.totalDels.rejected || 0 } 个</span>
            </span>
          )
        }
      }
    },
    methods: {
      openVision(row) {
        this.row = row
        this.versionvisible = true
      },
      loadVisionData() {
        const requestParameters = {
          needCount: false,
          pageNo: 1,
          pageSize: 99999,
          query: {
            id: this.row.id
          }
        }
        this.loadingVersion = true
        return masterWorkPlanFindListByMainHistoryList(requestParameters).then(res=>{
          res.data.forEach(item=>{
            if(item.versionNumber) {
              item.versionNumber = 'v' + item.versionNumber
            }
          })
          this.loadingVersion = false
          return res
        })
      },
      exportExcel() {
        let data = {
          pageNo: 1,
          pageSize: 99999,
          needCount: false,
          query:{ ...this.queryParam }
        }
        this.exportLoading = true
        masterWorkPlanExportExcelByPage(data).then(res=>{
          this.exportLoading = false
          this.handleDownload(res)
        }).catch(()=>{
          this.exportLoading = false
        })

      },
      handleApproveDetail(record) {
        this.visibleApprove = true
        this.businessId = record.id
      },
      search(val) {
        if(val.startEndDate&&val.startEndDate.length) {
          val.submitDateStart = val.startEndDate[0].format("YYYY-MM-DD")
          val.submitDateEnd = val.startEndDate[1].format("YYYY-MM-DD")
        }
        this.queryParam = val
        this.$refs.table.refresh(true)
      },

      getOptimizationTotal() {
        masterWorkPlanCount({ query: { ...this.queryParam } }).then((res) => {
          this.totalDels = res.data
        })
      },
      handleAdjust(row) {
            this.$router.push({
              path: '/businessAffairs/workPlan/workMasterPlanAed',
              query: {
                id: row.id,
                pageType: 'change',
                projectId: row.projectId,
                completedDate: row.completedDate,
                winningTime: row.winningTime
              }
            })
      },
      handleDels(row) {
        // this.spinning = true
        // masterWorkPlanChecked(row.projectId).then(res => {
        //   this.spinning = false
            this.$router.push({
              path: '/businessAffairs/workPlan/workMasterPlanAed',
              query: {
                id: row.id,
                pageType: 'view',
                projectId: row.projectId,
                completedDate: row.completedDate,
                winningTime: row.winningTime
              }
            })
        // }).catch(()=>{
        //   this.spinning = false
        // })
      },
      lookHistory(row) {
        this.versionvisible = false
        this.$router.push({
          path: '/businessAffairs/workPlan/workMasterPlanAed',
          query: {
            id: row.id,
            pageType: 'view',
            isHistory: 1,
            projectId: row.projectId,
            completedDate: row.completedDate,
            winningTime: row.winningTime
          }
        })
      },
      handleEdit(row) {
        // this.spinning = true
      // masterWorkPlanChecked(row.projectId).then(res=>{
      //   this.spinning = false
          this.$router.push({
            path: '/businessAffairs/workPlan/workMasterPlanAed',
            query: {
              id: row.id,
              pageType: 'edit',
              projectId: row.projectId,
              completedDate: row.completedDate,
              winningTime: row.winningTime
            }
          })
      // }).catch(()=>{
      //   this.spinning = false
      // })
      },
      /**
       * @description: 下载
       * @return {void}
       */
      handleDownload(res) {
        const blob = new Blob([res.data])
        const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = filename // 获取文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() //点击下载
        document.body.removeChild(downloadElement) //下载完成移除元素
        window.URL.revokeObjectURL(href) //释放blob对象
      }
    }
  }
</script>
